package com.hitqz.robot.driver.netty.strategy;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.driver.dto.AirQualityDto;
import com.hitqz.robot.driver.netty.protocol.message.TrackMessage;
import com.hitqz.robot.driver.util.TrackCacheUtil;
import com.hitqz.robot.driver.util.TrackToolkit;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 消息解析，只做信息保存或者发布事件，不做业务逻辑处理
 */
@Slf4j
public class Track65Strategy implements TrackMessageHandlerStrategy {

    @Override
    public void handle(ChannelHandlerContext ctx, TrackMessage message) {
        // 获取消息中的数据
        byte[] data = message.getData();
        // 分配 ByteBuf 并将数据写入
        ByteBuf buffer = ctx.alloc().buffer(data.length);
        buffer.writeBytes(data);
        byte mainCmd = buffer.readByte();
        byte subCmd = buffer.readByte();
        try {
            byte result = buffer.readByte();
            TrackToolkit.release(message, true, result == (byte) 0);
        } catch (Exception e) {
            log.error("[track]deal  {}{}-message error,{}",String.format("%02x",mainCmd),String.format("%02x",subCmd),e.getMessage());
        } finally {
            // 确保 ByteBuf 在使用完后释放，防止内存泄漏
            buffer.release();
        }
    }

}
